<?php

namespace frontend\modules\resource\controllers;

use frontend\modules\common\controllers\BaseController;
use frontend\modules\common\models\BaseParam;
use frontend\modules\resource\logic\ResourceClassesLogic;
use Yii;
use common\models\ResourceClasses;
use frontend\modules\resource\models\ResourceClassesSearch;
use yii\web\NotFoundHttpException;

/**
 * ResourceClassesController implements the CRUD actions for ResourceClasses model.
 */
class ResourceClassesController extends BaseController
{
    /**
     * Lists all ResourceClasses models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ResourceClassesSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', compact('searchModel', 'dataProvider'));
    }


    /**
     * Creates a new ResourceClasses model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new ResourceClasses();
        $model->setScenario(ResourceClasses::SCENARIOS_CATE_CU);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['finish']);
        }

        return $this->render('create', compact('model'));
    }

    /**
     * Updates an existing ResourceClasses model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $model->setScenario(ResourceClasses::SCENARIOS_CATE_CU);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['finish']);
        }

        return $this->render('update', compact('model'));
    }


    /**
     * Finds the ResourceClasses model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ResourceClasses the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ResourceClasses::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    /**资源分类状态
     *
     * @return string
     */
    public function actionCateStatus()
    {
        try {
            $id = Yii::$app->request->post('id');
            $res = (new ResourceClassesLogic())->UpdateCateStatus($id);
            if (!$res) {
                throw new NotFoundHttpException('修改失败');
            }
            return $this->suc(['msg' => '修改成功']);
        } catch (NotFoundHttpException $e) {

            return $this->err($e->getMessage());
        }
    }
}
